var pageSize=20;  //每页显示的记录条数
var curPage=0;   //显示第curPage页
var len;         //总行数
var page;        //总页数


$(function () {

    $("body").css("opacity","1")

    $("#xsxinxi").click(function(){
        $(this).css({"background-color":"#007dd2","color":"white"})
        $("#jsxinxi").css({"background-color":"white","color":"black"})
        $("#xsxinxixg").css({"background-color":"white","color":"black"})
        $("#jsxinxixg").css({"background-color":"white","color":"black"})
        $("#zengjia").css({"background-color":"white","color":"black"})
        $("#kongzhi1").show(500)
        $("#kongzhi2").hide(500)
        $("#kongzhi3").hide(500)
        $("#kongzhi4").hide(500)
        $("#kongzhi5").hide(500)
    });

    $("#jsxinxi").click(function(){
        $(this).css({"background-color":"#007dd2","color":"white"})
        $("#xsxinxi").css({"background-color":"white","color":"black"})
        $("#xsxinxixg").css({"background-color":"white","color":"black"})
        $("#jsxinxixg").css({"background-color":"white","color":"black"})
        $("#zengjia").css({"background-color":"white","color":"black"})
        $("#kongzhi1").hide(500)
        $("#kongzhi2").show(500)
        $("#kongzhi3").hide(500)
        $("#kongzhi4").hide(500)
        $("#kongzhi5").hide(500)
    });

    $("#xsxinxixg").click(function(){
        $(this).css({"background-color":"#007dd2","color":"white"})
        $("#xsxinxi").css({"background-color":"white","color":"black"})
        $("#jsxinxi").css({"background-color":"white","color":"black"})
        $("#jsxinxixg").css({"background-color":"white","color":"black"})
        $("#zengjia").css({"background-color":"white","color":"black"})
        $("#kongzhi1").hide(500)
        $("#kongzhi2").hide(500)
        $("#kongzhi3").show(500)
        $("#kongzhi4").hide(500)
        $("#kongzhi5").hide(500)
    });


    $("#jsxinxixg").click(function(){
        $(this).css({"background-color":"#007dd2","color":"white"})
        $("#xsxinxi").css({"background-color":"white","color":"black"})
        $("#jsxinxi").css({"background-color":"white","color":"black"})
        $("#xsxinxixg").css({"background-color":"white","color":"black"})
        $("#zengjia").css({"background-color":"white","color":"black"})
        $("#kongzhi1").hide(500)
        $("#kongzhi2").hide(500)
        $("#kongzhi3").hide(500)
        $("#kongzhi4").show(500)
        $("#kongzhi5").hide(500)
    });


    $("#zengjia").click(function(){
        $(this).css({"background-color":"#007dd2","color":"white"})
        $("#xsxinxi").css({"background-color":"white","color":"black"})
        $("#jsxinxi").css({"background-color":"white","color":"black"})
        $("#xsxinxixg").css({"background-color":"white","color":"black"})
        $("#jsxinxixg").css({"background-color":"white","color":"black"})
        $("#kongzhi1").hide(500)
        $("#kongzhi2").hide(500)
        $("#kongzhi3").hide(500)
        $("#kongzhi4").hide(500)
        $("#kongzhi5").show(500)
    });

    
    $.post("/xjxt/glygerenxinxicx",function (res) {

        var hehe = JSON.parse(res);
        console.log(hehe);
        $("#xsgerenxinxi").append("<tr id='first'>"+ "<td>" + "学号" +"</td>" + "<td>" + "姓名"+"</td>" +"<td>" + "性别" +"</td>"+ "<td>" +"身份证号" +"</td>"+ "<td>" +"名族" +"</td>"+ "<td>" +"层次" +"</td>" +"<td>" +"删除" +"</td>"+"</tr>");
        $.each(hehe, function (colIndex, col) {
            var th;
            th+="<td>" + "<a id='"+col.username+"' href='#'>" + col.username+ "</a>" + "</td>" + "<td>" +col.name+"</td>" + "<td>" +col.sex+"</td>" + "<td>" +col.ID+"</td>" +"<td>" +col.nation+"</td>" + "<td>" +col.cengci+"</td>" + "<td>" + "<button id='1"+col.username+"'>"+"删除"+ "</button>"+"</td>" ;
            $("#xsgerenxinxi").append("<tr>" + th + "</tr>");

            $("#"+col.username+"").click(function(){
                $.post("/xjxt/gerenxinxi?username="+ col.username +"",function(res){

                    var hehe = JSON.parse(res);
                    console.log(hehe);
                    $("#knumber").val(hehe[0].knumber);
                    $("#xnumber").val(hehe[0].xnumber);
                    $("#name").val(hehe[0].name);
                    $("#sex").val(hehe[0].sex);
                    $("#birthday").val(hehe[0].birthday);
                    $("#ID").val(hehe[0].ID);
                    $("#nation").val(hehe[0].nation);
                    $("#school").val(hehe[0].school);
                    $("#nschool").val(hehe[0].nschool);
                    $("#cengci").val(hehe[0].cengci);
                    $("#xuezhi").val(hehe[0].xuezhi);
                    $("#peiyang").val(hehe[0].peiyang);
                    $("#leibie").val(hehe[0].leibie);
                    $("#inday").val(hehe[0].inday);
                    $("#zhuangtai").val(hehe[0].zhuangtai);
                    $("#oldname").val(hehe[0].oldname);
                    $("#class1").val(hehe[0].class1);
                    $("#nianji").val(hehe[0].nianji);
                    $("#byyear").val(hehe[0].byyear);
                    $("#inschool").val(hehe[0].inschool);
                    $("#zhuanye").val(hehe[0].zhuanye);
                    $("#fangx").val(hehe[0].fangx);
                    $("#address").val(hehe[0].address);
                    $("#name1").val(hehe[0].name);
                    $("#xnumber1").val(hehe[0].xnumber);
                    $("#username").val(hehe[0].username);
                    $("#touxiang").attr("src",hehe[0].touxiang);
                    $("#yonghu").val(hehe[0].username);

                    $("#xsxinxixg").css({"background-color":"#007dd2","color":"white"})
                    $("#xsxinxi").css({"background-color":"white","color":"black"})
                    $("#jsxinxi").css({"background-color":"white","color":"black"})
                    $("#jsxinxixg").css({"background-color":"white","color":"black"})
                    $("#zengjia").css({"background-color":"white","color":"black"})
                    $("#kongzhi1").hide(500)
                    $("#kongzhi2").hide(500)
                    $("#kongzhi3").show(500)
                    $("#kongzhi4").hide(500)
                    $("#kongzhi5").hide(500)

                    $("#xgxsxx").click(function(){

                        var r = confirm("是否修改该学生信息");
                        console.log(r);
                        if(r == true) {
                            $.post("/xjxt/glyxueshengxiugai", {
                                "username": hehe[0].username,
                                "knumber": $("#knumber").val(),
                                "xnumber": $("#xnumber").val(),
                                "name": $("#name").val(),
                                "sex": $("#sex").val(),
                                "birthday": $("#birthday").val(),
                                "ID": $("#ID").val(),
                                "nation": $("#nation").val(),
                                "school": $("#school").val(),
                                "nschool": $("#nschool").val(),
                                "cengci": $("#cengci").val(),
                                "xuezhi": $("#xuezhi").val(),
                                "peiyang": $("#peiyang").val(),
                                "leibie": $("#leibie").val(),
                                "inday": $("#inday").val(),
                                "zhuangtai": $("#zhuangtai").val(),
                                "oldname": $("#oldname").val(),
                                "class1": $("#class1").val(),
                                "nianji": $("#nianji").val(),
                                "byyear": $("#byyear").val(),
                                "inschool": $("#inschool").val(),
                                "zhuanye": $("#zhuanye").val(),
                                "fangx": $("#fangx").val(),
                                "address": $("#address").val()
                            }, function (res) {
                                if (res == "2") {
                                    alert("修改失败");
                                    window.location.reload()
                                }
                                if (res == "1") {
                                    alert("修改成功");
                                    window.location.reload()
                                }

                            });


                        }
                    });

                });



            });

            $("#1"+col.username+"").click(function () {

                var r = confirm("是否删除该学生信息");
                console.log(r);
                if(r == true)
                {
                    $.post("/xjxt/xueshengshanchu?username="+ col.username +"",function(res){
                        if(res == 1){
                            alert("恭喜您删除成功")
                            window.location.reload()
                        }
                        else{
                            alert("删除失败")
                            window.location.reload()
                        }
                    });
                }

            });



        });

        len =$("#xsgerenxinxi tr").length-1;   //去掉表头
        page=len % pageSize==0 ? len/pageSize : Math.floor(len/pageSize)+1;//根据记录条数，计算页数
        console.log("len:"+len+"page:"+page);
        document.getElementById("page").value=page;
        curPage=1;
        displayPage();//显示第一页
        $("#nextpage").click(function(){//下一页
            if(curPage<page){
                curPage+=1;
            }
            else{
                alert("已经是最后一页了");
            }
            displayPage();
        });
        $("#lastpage").click(function(){//上一页
            if(curPage>1){
                curPage-=1;
            }
            else{
                alert("已经是第一页了");
            }
            displayPage();
        });
        $("#npage").click(function(){//跳到固定某一页
            var npage=parseInt(document.getElementById("curPage").value);
            if(npage>page||npage<1){
                alert("该页不存在");
            }
            else{
                curPage=npage;
            }
            displayPage();
        });

    });



    $.post("/xjxt/jsdgerenxinxicx",function (res) {

        var hehe = JSON.parse(res);
        console.log(hehe);
        $("#jsgerenxinxi").append("<tr id='first1'>"+ "<td>" + "工号" +"</td>" + "<td>" + "姓名"+"</td>" +"<td>" + "性别" +"</td>"+ "<td>" +"身份证号" +"</td>"+ "<td>" +"名族" +"</td>"+ "<td>" +"学历" +"</td>"+ "<td>" + "删除" + "</td>" +"</tr>");
        $.each(hehe, function (colIndex, col) {
            var th;
            th+="<td>" + "<a id='"+col.username+"' href='#'>" + col.username+ "</a>" + "</td>" + "<td>" +col.name+"</td>" + "<td>" +col.sex+"</td>" + "<td>" +col.ID+"</td>" +"<td>" +col.nation+"</td>" + "<td>" +col.xueli+"</td>" + "<td>" + "<button id='1"+col.username+"'>"+"删除"+ "</button>"+"</td>" ;
            $("#jsgerenxinxi").append("<tr>" + th + "</tr>");

           $("#"+col.username+"").click(function(){
               $.post("/xjxt/jsgerenxinxi?username="+col.username+"",function(res){

                   var hehe = JSON.parse(res);
                   console.log(hehe);
                   $("#bsnumber").val(hehe[0].bsnumber);
                   $("#gnumber").val(hehe[0].gnumber);
                   $("#name2").val(hehe[0].name);
                   $("#sex1").val(hehe[0].sex);
                   $("#birthday1").val(hehe[0].birthday);
                   $("#ID1").val(hehe[0].ID);
                   $("#nation1").val(hehe[0].nation);
                   $("#school1").val(hehe[0].school);
                   $("#nschool1").val(hehe[0].nschool);
                   $("#xueli").val(hehe[0].xueli);
                   $("#xuezhi1").val(hehe[0].xuezhi);
                   $("#jiankang").val(hehe[0].jiankang);
                   $("#hunyin").val(hehe[0].hunyin);
                   $("#inday1").val(hehe[0].inday);
                   $("#zaibian").val(hehe[0].zaibian);
                   $("#oldname1").val(hehe[0].oldname);
                   $("#class2").val(hehe[0].class);
                   $("#nianji1").val(hehe[0].nianji);
                   $("#byyear1").val(hehe[0].byyear);
                   $("#inschool1").val(hehe[0].inschool);
                   $("#zhuanye1").val(hehe[0].zhuanye);
                   $("#address1").val(hehe[0].address);
                   $("#name3").val(hehe[0].name);
                   $("#gnumber1").val(hehe[0].gnumber);
                   $("#touxiang1").attr("src",hehe[0].touxiang);
                   $("#yonghu1").val(hehe[0].username);


                    $("#jsxinxixg").css({"background-color":"#007dd2","color":"white"})
                    $("#xsxinxi").css({"background-color":"white","color":"black"})
                    $("#jsxinxi").css({"background-color":"white","color":"black"})
                    $("#xsxinxixg").css({"background-color":"white","color":"black"})
                    $("#zengjia").css({"background-color":"white","color":"black"})
                    $("#kongzhi1").hide(500)
                    $("#kongzhi2").hide(500)
                    $("#kongzhi3").hide(500)
                    $("#kongzhi4").show(500)
                    $("#kongzhi5").hide(500)


                   $("#jsxinxixiugai").click(function(){

                       var r = confirm("是否修改该老师信息");
                       console.log(r);
                       if(r == true) {
                           $.post("/xjxt/glylaoshixiugai", {
                               "username": hehe[0].username,
                               "bsnumber": $("#bsnumber").val(),
                               "gnumber": $("#gnumber").val(),
                               "name": $("#name2").val(),
                               "sex": $("#sex1").val(),
                               "birthday": $("#birthday1").val(),
                               "ID": $("#ID1").val(),
                               "nation": $("#nation1").val(),
                               "school": $("#school1").val(),
                               "nschool": $("#nschool1").val(),
                               "xueli": $("#xueli").val(),
                               "xuezhi": $("#xuezhi1").val(),
                               "jiankang": $("#jiankang").val(),
                               "hunyin": $("#hunyin").val(),
                               "inday": $("#inday1").val(),
                               "zaibian": $("#zaibian").val(),
                               "oldname": $("#oldname1").val(),
                               "class1": $("#class2").val(),
                               "nianji": $("#nianji1").val(),
                               "byyear": $("#byyear1").val(),
                               "inschool": $("#inschool1").val(),
                               "zhuanye": $("#zhuanye1").val(),
                               "address": $("#address1").val()
                           }, function (res) {
                               if (res == "2") {
                                   alert("修改失败");
                                   window.location.reload()
                               }
                               if (res == "1") {
                                   alert("修改成功");
                                   window.location.reload()
                               }

                           });

                       }
                });



            });

        });

           $("#1"+col.username+"").click(function () {

               var r = confirm("是否删除该老师信息");
               console.log(r);
               if(r == true)
               {
                   $.post("/xjxt/laoshishanchu?username="+ col.username +"",function(res){
                       if(res == 1){
                           alert("恭喜您删除成功")
                           window.location.reload()
                       }
                       else{
                           alert("删除失败")
                           window.location.reload()
                       }
                   });
               }

           });


           
        });
    });

    $("#xueshengss1").change(function () {
        var searchText = $(this).val();//获取输入的搜索内容
        if (searchText != "") {

            $("#xsgerenxinxi tr:not('#first')").hide().filter(":contains('"+searchText+"')").show();
        }
        else {
            $("#xsgerenxinxi tr").show();
        }
    })
    $("#xueshengss").click(function () {
        $("searchText").change();
    })

    $("#laoshiss1").change(function () {
        var searchText = $(this).val();//获取输入的搜索内容
        if (searchText != "") {

            $("#jsgerenxinxi tr:not('#first1')").hide().filter(":contains('"+searchText+"')").show();
        }
        else {
            $("#jsgerenxinxi tr").show();
        }
    })
    $("#laoshiss").click(function () {
        $("searchText").change();
    })


    $("#xuesheng").click(function () {

        $(this).css({"background-color":"#007dd2","color":"white"})
        $("#laoshi").css({"background-color":"white","color":"black"})
        $("#kongzhi6").show(500)
        $("#kongzhi7").hide(500)

    });

    $("#laoshi").click(function () {

        $(this).css({"background-color":"#007dd2","color":"white"})
        $("#xuesheng").css({"background-color":"white","color":"black"})
        $("#kongzhi7").show(500)
        $("#kongzhi6").hide(500)

    });

    $("#xzxueshengxx").click(function(){

        var r = confirm("是否新增该学生信息");
        console.log(r);
        if(r == true) {
            $.post("/xjxt/glyxzxuesheng", {
                "knumber": $("#knumber0").val(),
                "xnumber": $("#xnumber0").val(),
                "name": $("#name0").val(),
                "sex": $("#sex0").val(),
                "birthday": $("#birthday0").val(),
                "ID": $("#ID0").val(),
                "nation": $("#nation0").val(),
                "school": $("#school0").val(),
                "nschool": $("#nschool0").val(),
                "cengci": $("#cengci0").val(),
                "xuezhi": $("#xuezhi0").val(),
                "peiyang": $("#peiyang0").val(),
                "leibie": $("#leibie0").val(),
                "inday": $("#inday0").val(),
                "zhuangtai": $("#zhuangtai0").val(),
                "oldname": $("#oldname0").val(),
                "class1": $("#class10").val(),
                "nianji": $("#nianji0").val(),
                "byyear": $("#byyear0").val(),
                "inschool": $("#inschool0").val(),
                "zhuanye": $("#zhuanye0").val(),
                "fangx": $("#fangx0").val(),
                "address": $("#address0").val(),
                "touxiang": $("#touxiang0").val()
            }, function (res) {
                if (res == "2") {
                    alert("新增失败");
                    window.location.reload()
                }
                if (res == "1") {
                    alert("新增成功");
                    window.location.reload()
                }

            });
            var user = $("#xnumber0").val();
            $("#yonghu3").val(user);
            $("#shangchuan4").submit();
        }
    });

    $("#jsxxxinzeng").click(function(){

        var r = confirm("是否新增该老师信息");
        console.log(r);
        if(r == true) {
            $.post("/xjxt/glyxzlaoshi", {
                "bsnumber": $("#bsnumber0").val(),
                "gnumber": $("#gnumber0").val(),
                "name": $("#name20").val(),
                "sex": $("#sex10").val(),
                "birthday": $("#birthday10").val(),
                "ID": $("#ID10").val(),
                "nation": $("#nation10").val(),
                "school": $("#school10").val(),
                "nschool": $("#nschool10").val(),
                "xueli": $("#xueli0").val(),
                "xuezhi": $("#xuezhi10").val(),
                "jiankang": $("#jiankang0").val(),
                "hunyin": $("#hunyin0").val(),
                "inday": $("#inday10").val(),
                "zaibian": $("#zaibian0").val(),
                "oldname": $("#oldname10").val(),
                "class1": $("#class20").val(),
                "nianji": $("#nianji10").val(),
                "byyear": $("#byyear10").val(),
                "inschool": $("#inschool10").val(),
                "zhuanye": $("#zhuanye10").val(),
                "address": $("#address0").val(),
                "touxiang": $("#touxiang10").val()
            }, function (res) {
                if (res == "2") {
                    alert("新增失败");
                    window.location.reload()
                }
                if (res == "1") {
                    alert("新增成功");
                    window.location.reload()
                }

            });
            var user = $("#gnumber0").val();
            $("#yonghu2").val(user);
            $("#shangchuan3").submit();
        }
    });


    $("#wenjian").change(function() {
        var $file = $(this);
        var fileObj = $file[0];
        var windowURL = window.URL || window.webkitURL;
        var dataURL;
        var $img = $("#touxiang");
        if(fileObj && fileObj.files && fileObj.files[0]){
            dataURL = windowURL.createObjectURL(fileObj.files[0]);
            $img.attr('src',dataURL);
        }else{
            dataURL = $file.val();
            var imgObj = document.getElementById("touxiang");
            imgObj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)";
            imgObj.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = dataURL;

        }
    });

    $("#wenjian1").change(function() {
        var $file = $(this);
        var fileObj = $file[0];
        var windowURL = window.URL || window.webkitURL;
        var dataURL;
        var $img = $("#touxiang1");
        if(fileObj && fileObj.files && fileObj.files[0]){
            dataURL = windowURL.createObjectURL(fileObj.files[0]);
            $img.attr('src',dataURL);
        }else{
            dataURL = $file.val();
            var imgObj = document.getElementById("touxiang1");
            imgObj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)";
            imgObj.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = dataURL;

        }
    });


    $("#wenjian2").change(function() {
        var $file = $(this);
        var fileObj = $file[0];
        var windowURL = window.URL || window.webkitURL;
        var dataURL;
        var $img = $("#touxiang2");
        if(fileObj && fileObj.files && fileObj.files[0]){
            dataURL = windowURL.createObjectURL(fileObj.files[0]);
            $img.attr('src',dataURL);
        }else{
            dataURL = $file.val();
            var imgObj = document.getElementById("touxiang2");
            imgObj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)";
            imgObj.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = dataURL;

        }
    });


    $("#wenjian3").change(function() {
        var $file = $(this);
        var fileObj = $file[0];
        var windowURL = window.URL || window.webkitURL;
        var dataURL;
        var $img = $("#touxiang3");
        if(fileObj && fileObj.files && fileObj.files[0]){
            dataURL = windowURL.createObjectURL(fileObj.files[0]);
            $img.attr('src',dataURL);
        }else{
            dataURL = $file.val();
            var imgObj = document.getElementById("touxiang3");
            imgObj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)";
            imgObj.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = dataURL;

        }
    });

});

function displayPage(){
    var begin=(curPage-1)*pageSize;//起始记录号
    var end = begin + pageSize;
    console.log("  begin:"+len+"   end:"+end);
    if(end > len ) end=len;
    $("#xsgerenxinxi tr").hide();
    $("#xsgerenxinxi tr").each(function(i){
        if(i-1>=begin && i-1<end)//显示第page页的记录
        {
            $("#first").show();
            $(this).show();
            document.getElementById("curPage").value=curPage;
        }
    });

}

function pageSize2(){
    curPage=0;   //显示第curPage页
    pageSize=parseInt(document.getElementById("pageSize").value);
    len =$("#xsgerenxinxi tr").length-1;   //去掉表头
    page=len % pageSize==0 ? len/pageSize : Math.floor(len/pageSize)+1;//根据记录条数，计算页数
    console.log("len:"+len+"   page:"+page);
    document.getElementById("page").value=page;
    curPage=1;
    displayPage();//显示第一页
}
